import mDialogVue from './main.vue'
import Vue from 'vue'

const MDialogConstructor = Vue.extend(mDialogVue)
let instance = null

const initInstance = () => {
  if (instance) return instance
  instance = new MDialogConstructor()
  document.body.appendChild(instance.$mount().$el)
  return instance
}

const MDialogInstance = options => {
  const instance = initInstance()
  instance.showDialog(options)
  if (options.type) {
    return new Promise((resolve, reject) => {
      instance.$on('confirmEvent', () => {
        resolve()
      })
      instance.$on('refuseEvent', () => {
        reject(new Error('Error'))
      })
    })
  }
}

export default MDialogInstance
export { MDialogInstance }
